//
//  GuanZhuListCell.m
//  MPTLive
//
//  Created by HouGeng on 16/1/27.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "GuanZhuListCell.h"
#import "AnimateTools.h"

#define Kong (15)

@interface GuanZhuListCell()

@property (nonatomic, strong) TPImageView *imageHead;

@property (nonatomic, strong) TPLabel *labName;

@property (nonatomic, strong) UILabel *LabContent;

@property (nonatomic, strong) TPButton *butGuanZhu;

@end

@implementation GuanZhuListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _imageHead = [TPImageView getWithFrame:CGRectMake(Kong, Kong, 40, 40)];
        [self.contentView addSubview:_imageHead];
        
        _labName = [TPLabel getWithFrame:CGRectMake(CGRectGetMaxX(_imageHead.frame) + 10, Kong, 200, 20) text:@"" textColor:RGBAllColor(0x282828) textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f] textAlignment:NSTextAlignmentLeft];
        [self.contentView addSubview:_labName];
        
        _LabContent = [TPLabel getWithFrame:CGRectMake(CGRectGetMaxX(_imageHead.frame) + 10, CGRectGetMaxY(_labName.frame)+ 5, 200, 15) text:@"" textColor:RGBAllColor(0x666666) textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:12.0f] textAlignment:NSTextAlignmentLeft];
        [self.contentView addSubview:_LabContent];
        
        _butGuanZhu = [TPButton getWithFrame:CGRectMake(screenW - Kong - 28, 21, 28, 28) target:self action:@selector(GuanZhuClicked)];
        [self.contentView addSubview:_butGuanZhu];
        
        /// 线
        TPLabel *labLine = [TPLabel getWithFrame:CGRectMake(Kong, 70 - 0.5, screenW - 2 * Kong, [AnimateTools scaleAdaptWidth:0.5])];
        labLine.backgroundColor = RGBAllColor(0xDDDDDD);
        [self.contentView addSubview:labLine];
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic
{
    if (_dic != dic)
    {
        _dic = dic;
        
        /// 名称
        _labName.text = _dic[@"user"][@"nick"];
        
        /// 头像
        [_imageHead sd_setImageWithURL:[NSURL URLWithString:_dic[@"user"][@"icon"]] placeholderImage:nil];

        /// 签名
        _LabContent.text = _dic[@"user"][@"ext"][@"desc"];
        
        /// 用户关系
        switch ([_dic[@"relation"] integerValue])
        {
            case 0: // 已经关注
                
                [_butGuanZhu setImage:[UIImage imageNamed:@"weiguanzhu"] forState:UIControlStateNormal];
                break;
                
            case 1: // 已经关注
                
                [_butGuanZhu setImage:[UIImage imageNamed:@"yiguanzhu"] forState:UIControlStateNormal];
                break;
                
            case 2: // 没关注
                
                [_butGuanZhu setImage:[UIImage imageNamed:@"weiguanzhu"] forState:UIControlStateNormal];
                break;
                
            case 3: // 相互关注
                
                [_butGuanZhu setImage:[UIImage imageNamed:@"xianghuguanzhu"] forState:UIControlStateNormal];
                break;
                
            default:
                break;
        }
    }
}

- (void)GuanZhuClicked
{
    /// 用户关系
    if ([_dic[@"relation"] integerValue] == 3) /// 相互关注变成我没关注
    {
        /// 提示
        UIAlertView *alent = [[UIAlertView alloc] initWithTitle:@"确认不再关注TA吗?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alent show];
        return;
    }
    else if ([_dic[@"relation"] integerValue] == 2) /// 我没关注变成相互关注
    {
        [_butGuanZhu setImage:[UIImage imageNamed:@"xianghuguanzhu"] forState:UIControlStateNormal];
        /// 改变数据原
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_dic];
        [dic setValue:@([_dic[@"relation"] integerValue] + 1) forKey:@"relation"];
        _dic = [dic copy];
    }
    else if ([_dic[@"relation"] integerValue] == 1) /// 我关注了他变成没有
    {
        /// 提示
        UIAlertView *alent = [[UIAlertView alloc] initWithTitle:@"确认不再关注TA吗?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alent show];
        return;
    }
    else if ([_dic[@"relation"] integerValue] == 0) ///  没有关系
    {
        [_butGuanZhu setImage:[UIImage imageNamed:@"yiguanzhu"] forState:UIControlStateNormal];
        /// 改变数据原
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_dic];
        [dic setValue:@([_dic[@"relation"] integerValue] + 1) forKey:@"relation"];
        _dic = [dic copy];
    }
    
    if (self.block)
    {
        self.block(_indexpath);
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:
        {
            NSLog(@"Cancel Button Pressed");
            
            break;
        }
        default:
        {
            [_butGuanZhu setImage:[UIImage imageNamed:@"weiguanzhu"] forState:UIControlStateNormal];
            /// 改变数据原
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_dic];
            [dic setValue:@([_dic[@"relation"] integerValue] - 1) forKey:@"relation"];
            _dic = [dic copy];
            
            if (self.block)
            {
                self.block(_indexpath);
            }
            
            break;
        }
    }
}

@end
